package com.hehanpeng.springbootapi.service;

import com.hehanpeng.springbootapi.domain.Book;
import com.hehanpeng.springbootapi.domain.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hehanpeng
 * @date 2018/4/24 11:04
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;


    @Override
    public List<Book> findAllBooks() {
        List<Book> all = bookRepository.findAll();
        return all;
    }

    @Override
    public Book getBookById(Long id) {
        Book book = bookRepository.findById(id).orElse(null);
        return book;
    }

    @Override
    public Book saveBook(Book book) {
        Book save = bookRepository.save(book);
        return save;
    }

    @Override
    public Book updateBook(Book book) {
        Book save = bookRepository.save(book);
        return save;
    }

    @Override
    public void deleteBookById(Long id) {
        bookRepository.deleteById(id);
    }

    @Override
    public void deleteAllBooks() {
        bookRepository.deleteAll();
    }
}
